/*
 * ResultViewerForm.java
 *
 * Created on 24 kwiecień 2008, 10:25
 */
package flp.gui;

import flp.solvers.Solver;
import java.awt.BorderLayout;
import javax.swing.JPanel;

/**
 *
 * @author  Necro
 */
public class ResultViewerForm extends javax.swing.JFrame
{
    private Solver parentSolver;

    public ResultViewerForm(String title, JPanel chartPanel, JPanel infoPanel)
    {
        initComponents();
        this.setTitle(title);

        this.chartRootPanel.setLayout(new BorderLayout());
        this.infoRootPanel.setLayout(new BorderLayout());
        this.chartRootPanel.add(chartPanel, BorderLayout.CENTER);
        this.infoRootPanel.add(infoPanel, BorderLayout.CENTER);
    }

    public void setParentSolver(Solver parentSolver)
    {
        this.parentSolver = parentSolver;
    }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chartRootPanel = new javax.swing.JPanel();
        infoRootPanel = new javax.swing.JPanel();
        cancelButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        chartRootPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        chartRootPanel.setLayout(new javax.swing.BoxLayout(chartRootPanel, javax.swing.BoxLayout.LINE_AXIS));

        infoRootPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        infoRootPanel.setLayout(new javax.swing.BoxLayout(infoRootPanel, javax.swing.BoxLayout.LINE_AXIS));

        cancelButton.setText("Przerwij");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        exitButton.setText("Zamknij");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(chartRootPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 767, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(infoRootPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 690, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chartRootPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(infoRootPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        parentSolver.setStopped();
        dispose();
}//GEN-LAST:event_exitButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        parentSolver.setStopped();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /** Metoda tworzaca nowa forme. */
    public static ResultViewerForm create(final String title, final JPanel chartPanel,
            final JPanel infoPanel)
    {

        class FormRunnable implements Runnable
        {
            private ResultViewerForm view;

            FormRunnable(String title, JPanel chartPanel, JPanel infoPanel)
            {
                view = new ResultViewerForm(title, chartPanel, infoPanel);
            }

            public void run()
            {
                view.setVisible(true);
            }

            public ResultViewerForm getView()
            {
                return view;
            }
        }
        FormRunnable tmpForm = new FormRunnable(title, chartPanel, infoPanel);
        java.awt.EventQueue.invokeLater(tmpForm);
        return tmpForm.getView();
    }

    /**
     * Metoda wstawiajaca wykres w odpowiednie miejsce na formie.
     * 
     * @param newChartPanel Wstawiany panel.
     */
    public void setChart(JPanel newChartPanel)
    {
        this.chartRootPanel.removeAll();
        this.chartRootPanel.add(newChartPanel);
        this.chartRootPanel.repaint();
    }

    public void disableCancelButton()
    {
        cancelButton.setEnabled(false);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel chartRootPanel;
    private javax.swing.JButton exitButton;
    private javax.swing.JPanel infoRootPanel;
    // End of variables declaration//GEN-END:variables
}
